package com.sgcc.ln.pmos.common.widget;

import android.content.Context;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lxj.xpopup.core.CenterPopupView;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.common.utils.UserManager;


public class TagOtherView extends CenterPopupView {
    TextView positive, txt_phone;
    public OnListener onClickListener;
    //    private VerificationCodeInputView inputView;
    private String MsgCode = "";
    TextView tv_resend, tv_time;
    LinearLayout linnear_time;


    private VerifyCodeView verify_view;

    public TagOtherView(Context context, OnListener onClickListener) {
        super(context);
        this.onClickListener = onClickListener;

    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.messagecheck_dialog_layout;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        tv_resend = (TextView) findViewById(R.id.tv_resend);
        tv_time = (TextView) findViewById(R.id.tv_time);
        linnear_time = (LinearLayout) findViewById(R.id.linnear_time);
        initTimer();
        txt_phone = (TextView) findViewById(R.id.txt_phone);
        if (UserManager.getUserBean() != null) {
            if (UserManager.getUserBean().getTelPhone() != null) {
                if (UserManager.getUserBean().getTelPhone().size() != 0) {
                    String phone = UserManager.getUserBean().getTelPhone().get(0).replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                    txt_phone.setText(phone);
                }
            }
        }
//        txt_phone.setText(UserManager.getUserBean().getTelPhone().get(0));
        positive = (TextView) findViewById(R.id.positive);
        positive.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (onClickListener != null) {
                    onClickListener.onPositiveClick(MsgCode);
                }
            }
        });
        findViewById(R.id.close_img).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                TagOtherView.this.dismiss();

            }
        });
//        inputView = (VerificationCodeInputView) findViewById(R.id.vciv_inputCode_code);
//        inputView.setOnInputListener(new VerificationCodeInputView.OnInputListener() {
//            @Override
//            public void onComplete(String code) {
//                MsgCode = code;
//                if (onClickListener != null) {
//                    onClickListener.onCompleteClick(code);
//                }
//            }
//        });

        verify_view = (VerifyCodeView) findViewById(R.id.verify_view);
        verify_view.setInputCompleteListener(new VerifyCodeView.InputCompleteListener() {
            @Override
            public void inputComplete() {
                MsgCode = verify_view.getEditContent();
                if (onClickListener != null) {
                    onClickListener.onCompleteClick(MsgCode);
                }
            }

            @Override
            public void invalidContent() {

            }
        });


        tv_resend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickListener != null) {
                    onClickListener.onReSendClick();
                    initTimer();
                }
            }
        });


        getData();


    }

    private void getData() {
    }

    public void clearCode() {
//        inputView.clearCode();
        verify_view.clearEdit();
    }

    @Override
    protected void onShow() {
        super.onShow();

    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

//    @Override
//    protected int getPopupHeight() {
//        return (int) (XPopupUtils.getScreenHeight(getContext()) * .5f);
//    }

//    @Override
//    protected int getPopupWidth() {
//        return (int) (XPopupUtils.getScreenHeight(getContext()) * .9f);
//    }


    public interface OnListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String code);

        /**
         * 输入完成事件
         */
        public void onCompleteClick(String code);

        /**
         * 点击重新发送事件
         */
        public void onReSendClick();

    }


    /**
     * 根据状态码切换框内UI
     * 1是刚刚发送验证码  等待用户输入状态 显示读秒  不显示重新发送
     * 2是读秒结束    等待用户重新发送  显示重新发送  不显示读秒
     *
     * @param state
     */
    private void changeMsgStateUI(int state) {
        switch (state) {
            case 1:
                tv_resend.setVisibility(GONE);
                linnear_time.setVisibility(VISIBLE);
                break;
            case 2:
                tv_resend.setVisibility(VISIBLE);
                linnear_time.setVisibility(GONE);
                break;
        }
    }

    private void initTimer() {
        changeMsgStateUI(1);
        CountDownTimer timer = new CountDownTimer(120000, 1000) {
            public void onTick(long millisUntilFinished) {
                tv_time.setText(millisUntilFinished / 1000 + "s");
            }

            public void onFinish() {
                tv_time.setText("120s");
                changeMsgStateUI(2);
            }
        };
        timer.start();
    }
}